//https://router.vuejs.org/zh/guide/essentials/dynamic-matching.html

import {createRouter,createWebHashHistory} from 'vue-router'

import main from '~/pages/layout.vue'
import Index from '~/pages/index.vue'
import NotFound from '~/pages/404.vue'
import Login from '~/pages/login.vue'

import DeviceInfo from '~/pages/views/device_info.vue'
import Reboot from '~/pages/views/reboot.vue'
import ChannelStatus from '~/pages/views/channel_status.vue'
import RecordingQurey from '~/pages/views/recording_qurey.vue'
import Set from '~/pages/views/set.vue'
import User from '~/pages/views/user.vue'

import ApiTest from '~/pages/views/apitest.vue'

const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes:[
        { path:'/', name: 'root', redirect:'/login'},
        { path:'/login', name: 'login', component:Login},
        { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound},
        // { path:'/home', name: 'main', redirect:'/main/0'},

        { path:'/home', 
          component:main,
          name: 'home',
          redirect:'/home/0',
          meta: { title: '主页' },
          children: [
            { path: '0', component: DeviceInfo, name: 'deviceinfo', meta: { title: '设备信息' }},
            { path: '1', component: ChannelStatus, name: 'ChannelStatus1', meta: { title: '通道状态' }},
            { path: '2', component: RecordingQurey, name: 'RecordingQurey', meta: { title: '录音查询' }},
            { path: '3', component: Set, name: 'Set', meta: { title: '系统设置' }},
            { path: '4', component: ApiTest, name: 'ApiTest', meta: { title: 'Api Test' }},
            { path: '5', component: User, name: 'User', meta: { title: '用户管理' }},
            { path: '6', component: Reboot, name: 'reboot', meta: { title: '重启' }},
          ]
        },
    ],
})
router.beforeEach((to,from,next)=>{
  const token = localStorage.getItem('token')
  if(to.path === '/main' && !token) {
    next(false);
  } else {
    next();
  }

})
export default router